﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using System.Net;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using CloudStorage_Backend.exception;

namespace CloudStorage_Backend.utils
{
    public class GlobalExceptionHandler
    {
       private readonly RequestDelegate _next;
       public GlobalExceptionHandler(RequestDelegate next) {
           _next = next;
       }
       public async Task Invoke(HttpContext context) {
           try {
               await _next(context);
           }
           catch(Exception e) {
               await HandleException(context, e);
           }
       }

       private static Task HandleException(HttpContext context, Exception exception) {
            ResponseResult<string> responseResult = new ResponseResult<string>();
            if (exception.GetType() == typeof(UserException))
            {
                switch (exception.Message)
                {
                    case "USER_NOT_FOUND":
                        responseResult = ResponseResult<string>.Error(ResultCode.USER_NOT_FOUND, "用户不存在！");
                        break;
                    case "NAME_OR_PASSWORD_WRONG":
                        responseResult = ResponseResult<string>.Error(ResultCode.NAME_OR_PASSWORD_WRONG, "账号或密码错误！");
                        break;
                    case "NAME_HAS_BEEN_USED":
                        responseResult = ResponseResult<string>.Error(ResultCode.NAME_HAS_BEEN_USED, "账号已被使用！");
                        break;
                    default:
                        responseResult = ResponseResult<string>.Error(ResultCode.UNKNOWN_ERROR, exception.Message);
                        break;
                }
            } else if(exception.GetType() == typeof(FileChunkException)) {
                switch (exception.Message) {
                    case "EMPTY_UPLOAD_REQUEST":
                        responseResult = ResponseResult<string>.Error(ResultCode.EMPTY_UPLOAD_REQUEST, "文件上传信息为空！");
                        break;
                    case "CHUNK_OVERFLOW":
                        responseResult = ResponseResult<string>.Error(ResultCode.CHUNK_OVERFLOW, "文件块溢出！");
                        break;
                    case "FILE_TOO_LARGE":
                        responseResult = ResponseResult<string>.Error(ResultCode.FILE_TOO_LARGE, "文件太大了！");
                        break;
                    case "FILECHUNKSIZE_WRONG":
                        responseResult = ResponseResult<string>.Error(ResultCode.FILECHUNKSIZE_WRONG, "文件块大小出错！");
                        break;
                    case "LASTCHUNK_WRONG":
                        responseResult = ResponseResult<string>.Error(ResultCode.LASTCHUNK_WRONG, "最后一块文件块出错！");
                        break;
                    case "SINGLECHUNK_WRONG":
                        responseResult = ResponseResult<string>.Error(ResultCode.SINGLECHUNK_WRONG, "单文件块出错！");
                        break;
                    default:
                        responseResult = ResponseResult<string>.Error(ResultCode.UNKNOWN_ERROR, exception.Message);
                        break;
                }
            } else if(exception.GetType() == typeof(FileException))
            {
                switch (exception.Message)
                {
                    case "DIRECTORY_NOT_FOUND":
                        responseResult = ResponseResult<string>.Error(ResultCode.DIRECTORY_NOT_FOUND, "找不到该文件夹！");
                        break;
                    case "INVALID_DIRECTORY_NAME":
                        responseResult = ResponseResult<string>.Error(ResultCode.INVALID_DIRECTORY_NAME, "非法的文件夹名！");
                        break;
                    case "FILE_NOT_FOUND":
                        responseResult = ResponseResult<string>.Error(ResultCode.FILE_NOT_FOUND, "找不到该文件！");
                        break;
                    case "FILE_HAS_EXIST":
                        responseResult = ResponseResult<string>.Error(ResultCode.FILE_HAS_EXIST, "该文件已存在！");
                        break;
                    case "FILE_TOO_LARGE":
                        responseResult = ResponseResult<string>.Error(ResultCode.FILE_TOO_LARGE, "该文件太大！");
                        break;
                    case "WRONG_PATH_FORMAT":
                        responseResult = ResponseResult<string>.Error(ResultCode.WRONG_PATH_FORMAT, "文件路径格式化错误！");
                        break;
                    default:
                        responseResult = ResponseResult<string>.Error(ResultCode.UNKNOWN_ERROR, exception.Message);
                        break;
                }
            }
           var result = JsonConvert.SerializeObject(responseResult);
           context.Response.ContentType = "application/json";
           context.Response.StatusCode = 200;
           return context.Response.WriteAsync(result);
       }
    }
}
